package com.newtype.meeting.client.widget.backend;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FileUploadField;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.form.Radio;
import com.extjs.gxt.ui.client.widget.form.RadioGroup;
import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;
import com.extjs.gxt.ui.client.widget.form.SimpleComboValue;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.newtype.meeting.client.AppEvents;
import com.newtype.meeting.client.model.AccountType;
import com.newtype.meeting.client.model.Degree;
import com.newtype.meeting.client.model.Title;
import com.newtype.meeting.shared.AccountDTO;
import com.newtype.meeting.shared.tools.RadioGroupConverter;

/**
 * 人员新增与修改窗体
 * @author 汪恭泽
 * May 31, 2011 10:23:15 AM
 */
public class PersonnelWindow extends Window {
	private BeanModel accountModel;

	public PersonnelWindow(AccountDTO accountDTO, List<Degree> degrees,
			final List<Title> titles) {
		setSize(600, 550);
		setClosable(true);
		setModal(true);
		setButtonAlign(HorizontalAlignment.CENTER);
		setLayout(new FormLayout());
		setScrollMode(Scroll.AUTO);

		FormData formData = new FormData("90%");
		FormPanel form = new FormPanel();
		form.setHeaderVisible(false);
		form.setBodyBorder(false);
		form.setButtonAlign(HorizontalAlignment.CENTER);

		Radio radio1 = new Radio();
		radio1.setBoxLabel("启用");
		radio1.setValueAttribute("1");

		Radio radio2 = new Radio();
		radio2.setBoxLabel("停用");
		radio2.setValueAttribute("2");

		RadioGroup status = new RadioGroup("status");
		status.setFieldLabel("帐号状态");
		status.setName("status");
		status.add(radio1);
		status.add(radio2);
		status.setValue(radio1);
		form.add(status, formData);

		final TextField<String> fullName = new TextField<String>();
		fullName.setFieldLabel("姓名");
		fullName.setName("fullName");
		fullName.setAllowBlank(false);
		fullName.getMessages().setBlankText("此栏位不可为空白");
		form.add(fullName);

		final TextField<String> accountID = new TextField<String>();
		accountID.setFieldLabel("帐号");
		accountID.setName("accountID");
		accountID.setAllowBlank(false);
		accountID.getMessages().setBlankText("此栏位不可为空白");
		form.add(accountID);

		final TextField<String> password = new TextField<String>();
		password.setFieldLabel("密码");
		password.setName("password");
		password.setPassword(true);
		password.setAllowBlank(false);
		password.getMessages().setBlankText("此栏位不可为空白");
		form.add(password);

		Radio radio3 = new Radio();
		radio3.setBoxLabel("男");
		radio3.setValueAttribute("1");

		Radio radio4 = new Radio();
		radio4.setBoxLabel("女");
		radio4.setValueAttribute("0");

		RadioGroup gender = new RadioGroup("gender");
		gender.setName("gender");
		gender.setFieldLabel("性别");
		gender.add(radio3);
		gender.add(radio4);
		gender.setValue(radio3);
		form.add(gender);

		DateField birthday = new DateField();
		birthday.setFieldLabel("生日");
		birthday.setName("birthday");
		form.add(birthday);

		DateField onlineDate = new DateField();
		onlineDate.setFieldLabel("入职日期");
		onlineDate.setName("onlineDATE");
		form.add(onlineDate);

		final ListStore<Title> titleStore = new ListStore<Title>();
		titleStore.add(new ArrayList<Title>());

		final ComboBox<Title> titleCombo = new ComboBox<Title>();
		titleCombo.setFieldLabel("职称");
		titleCombo.setEmptyText("请选择一个职称...");
		titleCombo.setEditable(false);
		titleCombo.setTriggerAction(TriggerAction.ALL);
		titleCombo.setDisplayField("titleCaption");
		titleCombo.setValueField("id");
		titleCombo.setStore(titleStore);

		final SimpleComboBox<String> degree = new SimpleComboBox<String>();
		degree.setFieldLabel("职级");
		degree.setEmptyText("请选择一个职级...");
		degree.setName("degree");
		degree.setTriggerAction(TriggerAction.ALL);
		degree.setEditable(false);
		for (Degree d : degrees) {
			degree.add(String.valueOf(d.getDegreeID()));
		}
		degree
				.addSelectionChangedListener(new SelectionChangedListener<SimpleComboValue<String>>() {
					@Override
					public void selectionChanged(
							final SelectionChangedEvent<SimpleComboValue<String>> se) {
						titleCombo.clearSelections();
						titleCombo.getStore().removeAll();

						List<Title> list = new ArrayList<Title>();

						Iterator<Title> iter = titles.iterator();
						while (iter.hasNext()) {
							Title t = iter.next();
							if (String.valueOf(t.getDegree()).equals(
									se.getSelectedItem().getValue())) {
								list.add(t);
							}
						}
						titleStore.add(list);
					}
				});
		form.add(degree);
		form.add(titleCombo);

		final HiddenField<String> deptID = new HiddenField<String>();
		deptID.setFieldLabel("部门ID");
		deptID.setName("deptID");
		form.add(deptID);

		final TextField<String> deptName = new TextField<String>();
		deptName.setFieldLabel("专职部门");
		deptName.setName("deptName");
		deptName.addListener(Events.OnClick, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				Map<String, Field<String>> department = new HashMap<String, Field<String>>();
				department.put("pDeptID", deptID);
				department.put("pDeptName", deptName);
				Dispatcher.forwardEvent(AppEvents.ToDeptTreeSingleWindow,
						department);
			}
		});
		form.add(deptName);

		final HiddenField<String> partTimeID = new HiddenField<String>();
		partTimeID.setFieldLabel("部门ID");
		partTimeID.setName("deptID");
		form.add(partTimeID);

		final TextArea partTimeName = new TextArea();
		partTimeName.setFieldLabel("兼职部门");
		partTimeName.setName("byWorkDeptID");
		partTimeName.addListener(Events.OnClick, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				Map<String, Field<String>> department = new HashMap<String, Field<String>>();
				department.put("deptID", partTimeID);
				department.put("deptName", partTimeName);
				Dispatcher.forwardEvent(AppEvents.ToDeptTreeMultiWindow,
						department);
			}
		});
		form.add(partTimeName);

		TextArea workDuties = new TextArea();
		workDuties.setFieldLabel("工作职掌");
		workDuties.setName("workDuties");
		form.add(workDuties, formData);

		TextField<String> officePhoneNO = new TextField<String>();
		officePhoneNO.setFieldLabel("公司电话");
		officePhoneNO.setName("officePhoneNO");
		form.add(officePhoneNO);

		TextField<String> officephoneEXT = new TextField<String>();
		officephoneEXT.setFieldLabel("分机");
		officephoneEXT.setName("officephoneEXT");
		form.add(officephoneEXT);

		TextField<String> email = new TextField<String>();
		email.setFieldLabel("Email");
		email.setName("email");
		form.add(email, formData);

		TextField<String> msn = new TextField<String>();
		msn.setFieldLabel("MSN");
		msn.setName("msn");
		form.add(msn, formData);

		TextField<String> skype = new TextField<String>();
		skype.setFieldLabel("Skype");
		skype.setName("skype");
		form.add(skype, formData);

		TextField<String> phone = new TextField<String>();
		phone.setFieldLabel("手机");
		phone.setName("phone");
		form.add(phone, formData);

		TextField<String> pager = new TextField<String>();
		pager.setFieldLabel("呼叫器");
		pager.setName("pager");
		form.add(pager, formData);

		TextField<String> homePhoneNO = new TextField<String>();
		homePhoneNO.setFieldLabel("家用电话");
		homePhoneNO.setName("homePhoneNO");
		form.add(homePhoneNO, formData);

		TextField<String> emergencyContact = new TextField<String>();
		emergencyContact.setFieldLabel("紧急联络人");
		emergencyContact.setName("emergencyContact");
		form.add(emergencyContact, formData);

		TextField<String> emergencyPhoneNO = new TextField<String>();
		emergencyPhoneNO.setFieldLabel("紧急联络电话");
		emergencyPhoneNO.setName("emergencyPhoneNO");
		form.add(emergencyPhoneNO, formData);

		final TextField<String> address = new TextField<String>();
		address.setFieldLabel("通讯地址");
		address.setName("address");
		form.add(address, formData);

		TextArea introduces = new TextArea();
		introduces.setFieldLabel("个人介绍");
		introduces.setName("introduces");
		form.add(introduces, formData);

		FileUploadField personIMG = new FileUploadField();
		personIMG.setFieldLabel("个人照片");
		personIMG.setName("personIMG");
		form.add(personIMG, formData);

		Radio radio5 = new Radio();
		radio5.setBoxLabel("是");
		radio5.setValueAttribute("1");

		Radio radio6 = new Radio();
		radio6.setBoxLabel("否");
		radio6.setValueAttribute("0");

		RadioGroup openTAG = new RadioGroup("openTAG");
		openTAG.setFieldLabel("公开个人资料");
		openTAG.add(radio5);
		openTAG.add(radio6);
		openTAG.setName("openTAG");
		openTAG.setValue(radio5);
		form.add(openTAG, formData);

		TextArea notes = new TextArea();
		notes.setFieldLabel("备注");
		notes.setName("notes");
		form.add(notes, formData);

		final SimpleComboBox<AccountType> typeCombo = new SimpleComboBox<AccountType>();
		typeCombo.setFieldLabel("用户类型");
		typeCombo.setTriggerAction(TriggerAction.ALL);
		typeCombo.setEditable(false);
		typeCombo.add(Arrays.asList(AccountType.values()));
		typeCombo.setName("accountType");
		typeCombo.setSimpleValue(AccountType.USER);
		typeCombo.setAllowBlank(false);
		form.add(typeCombo);

		//设置默认值
		accountDTO.setStatus("1");
		accountDTO.setGender("1");
		accountDTO.setOpenTAG("1");

		accountModel = BeanModelLookup.get().getFactory(AccountDTO.class)
				.createModel(accountDTO);

		final FormBinding formBinding = new FormBinding(form, true);

		//绑定状态RadioGroup
		FieldBinding statusFB = formBinding.getBinding(status);
		statusFB.setConverter(new RadioGroupConverter(status));
		//绑定性别RadioGroup
		FieldBinding genderFB = formBinding.getBinding(gender);
		genderFB.setConverter(new RadioGroupConverter(gender));
		//绑定公开个人资料openTAGRadioGroup
		FieldBinding openTAGFB = formBinding.getBinding(openTAG);
		openTAGFB.setConverter(new RadioGroupConverter(openTAG));
		//绑定职级SimpleComboBox 
		FieldBinding degreeBinding = new FieldBinding(degree, "degree") {
			@Override
			protected Object onConvertFieldValue(Object value) {
				return degree.getRawValue();
			}

			@Override
			protected Object onConvertModelValue(Object value) {
				String name = (String) value;
				return degree.findModel(name);
			}
		};
		formBinding.addFieldBinding(degreeBinding);

		//绑定用户类型
		FieldBinding typeBinding = new FieldBinding(typeCombo, "accountType") {
			@SuppressWarnings("unchecked")
			@Override
			protected Object onConvertFieldValue(Object value) {
				return ((SimpleComboValue<AccountType>) value).getValue();
			}

			@Override
			protected Object onConvertModelValue(Object value) {
				return typeCombo.findModel((AccountType) value);
			}
		};
		formBinding.addFieldBinding(typeBinding);

		formBinding.bind(accountModel);

		add(form);

		Button btnSend = new Button("送出");
		btnSend.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				AccountDTO accountDTO = accountModel.getBean();
				accountDTO.setDeptID(deptID.getValue());
				accountDTO.setByWorkDeptID(partTimeID.getValue());
				accountDTO.setTitle(titleCombo.getSelection().get(0).getId());
				Dispatcher.forwardEvent(AppEvents.SaveAccount, accountDTO);
				hide();
			}
		});

		FormButtonBinding buttonBinding = new FormButtonBinding(form);
		buttonBinding.addButton(btnSend);

		form.addButton(btnSend);
	}
}
